package com.holly.unit.deform;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.holly.unit.db.api.factory.PageFactory;
import com.holly.unit.db.api.factory.PageResultFactory;
import com.holly.unit.db.api.pojo.page.PageResult;
import com.holly.unit.deform.api.DeformAuthApi;
import com.holly.unit.deform.api.exception.DeformException;
import com.holly.unit.deform.api.exception.enums.DeformExceptionEnum;
import com.holly.unit.deform.api.pojo.auth.DesignFormAuthDTO;
import com.holly.unit.deform.context.DeformContext;
import com.holly.unit.deform.entity.DesignFormAuth;
import com.holly.unit.deform.service.DeformAuthService;
import com.holly.unit.query.api.QueryApi;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 类描述: 表单设计器字段权限表 操作类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/7 14:37
 */
public class DeformAuthOperator implements DeformAuthApi<DesignFormAuth>{

    private DeformAuthService authService = DeformContext.deformAuthService();

    private QueryApi queryApi = DeformContext.queryApi();

    @Override
    public PageResult<DesignFormAuth> page(DesignFormAuth designFormAuth, Map<String, String[]> paramMap) {
        QueryWrapper<DesignFormAuth> queryWrapper = queryApi.initQueryWrapper(designFormAuth, paramMap);
        Page<DesignFormAuth> page = authService.page(PageFactory.defaultPage(), queryWrapper);
        PageResult<DesignFormAuth> pageResult = PageResultFactory.createPageResult(page);
        return pageResult;
    }

    @Override
    public PageResult<DesignFormAuth> pageWithPermissionTypeAndDesCode(DesignFormAuth designFormAuth, Map<String, String[]> paramMap, String permissionType, String desformCode) {
        QueryWrapper<DesignFormAuth> queryWrapper = this.append(designFormAuth,paramMap);
        queryWrapper.eq("permission_type", permissionType);
        queryWrapper.eq("desform_code", desformCode);
        Page<DesignFormAuth> page = authService.page(PageFactory.defaultPage(), queryWrapper);
        PageResult<DesignFormAuth> pageResult = PageResultFactory.createPageResult(page);
        return pageResult;
    }

    /***
     * 条件拼接
     * @param designFormAuth
     * @return
     */

    private QueryWrapper<DesignFormAuth> append(DesignFormAuth designFormAuth, Map<String, String[]> paramMap) {
        QueryWrapper<DesignFormAuth> queryWrapper = queryApi.initQueryWrapper(designFormAuth, paramMap);
        return queryWrapper;
    }

    @Override
    public List<DesignFormAuthDTO> queryByTaskId(String desformCode, String taskId) {
        List<DesignFormAuthDTO> dtos = new ArrayList<>();
        List<DesignFormAuth> list = authService.queryByTaskId(desformCode,taskId);
        list.stream().forEach(entity->{
            DesignFormAuthDTO dto = new DesignFormAuthDTO();
            BeanUtil.copyProperties(entity,dto);
            dtos.add(dto);
        });
        return dtos;
    }

    @Override
    public JSONObject fetchButtonsAuth(String desformCode, String account) {
        return authService.queryButtonsAuth(desformCode,account);
    }

    @Override
    public void delById(Serializable id) {

        if (ObjectUtil.isNull(id)) {
            throw new DeformException(DeformExceptionEnum.DEFORM_QUERY_BY_ID_ERROR,id);
        }

        boolean b = authService.removeById(id);
        if (!b) {
            throw new DeformException(DeformExceptionEnum.DEFORM_DEL_PARAM_ERROR);
        }
    }
}
